/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogSearch.java
 *
 * Created on 10 Mei 10, 10:21:33
 */

package Riset1;
import java.util.ArrayList;
import amd.database.ConnectDB;
import amd.core.extraction.*;
import amd.core.data.*;
import Classes.HasilExtract;

/**
 *
 * @author Presario
 */
public class JDialogSearch extends javax.swing.JDialog {

    /** Creates new form JDialogSearch */
    public JDialogSearch(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        initComponents2();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        amdlsaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("amdlsaPU").createEntityManager();
        documentQuery = java.beans.Beans.isDesignTime() ? null : amdlsaPUEntityManager.createQuery("SELECT d FROM Document d");
        documentList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : documentQuery.getResultList();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldSearch = new javax.swing.JTextField();
        jButtonSearch = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jInternalFrame1.setVisible(true);

        jLabel1.setText("Keywords :");

        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonCancel)
                    .addComponent(jButtonSearch))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancel)
                .addContainerGap(90, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initComponents2(){
            jTextFieldSearch.setText("");
    }

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
 // TODO add your handling code here:
        String inQuery = jTextFieldSearch.getText();
        if(!inQuery.isEmpty()) {
            inQuery = inQuery.trim();
            ListTermFrequency lTf=TermsExtractor.extract(inQuery);
            lTf=TermFilter.filtering(lTf);
            lTf=Stemmer.stemming(lTf);
            String termQ[]=new String[lTf.size()];
            for(int i=0; i<lTf.size(); i++) {
                termQ[i]=lTf.get(i).getContent();
            }
//            ConnectDB conn=null;
//            try {
//                conn = new ConnectDB("jdbc:derby://localhost:1527/amdlsa","root","toor");
//            } catch (Exception ex) {
//                System.out.println(ex);
//                System.exit(0);
//            }

//            final ArrayList<ArrayList<String>> hQ=SearchTerm.Search(termQ, conn);
            final ArrayList<ArrayList<String>> hQ=queryRelevan(termQ);
            System.out.println("SETELAH SearchTerm.Search(termQuery, conn)");
            this.dispose();
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameHasilSearch(hQ).setVisible(true);
            }
        });

//            JFrameHasilSearch dialog = new JFrameHasilSearch(hQ);

        }
}//GEN-LAST:event_jButtonSearchActionPerformed

public ArrayList<ArrayList<String>> queryRelevan(String termQ[])
{
            ConnectDB conn=null;
            try {
                conn = new ConnectDB("jdbc:mysql://localhost:3306/db_amd_lsa", "root", "");
            } catch (Exception ex) {
                System.out.println(ex);
                System.exit(0);
            }

            final ArrayList<ArrayList<String>> hQ=SearchTerm.Search(termQ, conn);
            return hQ;
}



    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager amdlsaPUEntityManager;
    private java.util.List<Riset1.Document> documentList;
    private javax.persistence.Query documentQuery;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextFieldSearch;
    // End of variables declaration//GEN-END:variables

}
